OneHot

根据输入的索引值,在指定轴上生成独热编码(one-hot encoding)。

\[\begin{split}output_{i_1, i_2, \dots, i_{axis}, \dots, i_n} = \begin{cases} on\_value, & \text{if } i_{axis} = indices_{j} \\ off\_value, & \text{otherwise} \end{cases}\end{split}\]

其中,\(indices_{j}\) 为输入索引张量的元素,索引 \(j\) 与输出下标 \((i_1, \dots, i_{axis-1}, i_{axis+1}, \dots, i_n)\) 一一对应。 \(depth\) 表示 one-hot 向量的长度,\(axis\) 表示新维度插入的位置。

当启用 support_neg_index 时,若 \(indices_{j} < 0\),则执行如下修正:

\[indices_{j} = indices_{j} + depth\]
输入:
  • on_off - 包含**on_value**和**off_value**,表示独热编码中“热”和“冷”位置的值。

  • axis - 指定生成独热编码的轴。

  • depth - 独热编码的深度。

  • support_neg_index - 是否支持负索引。

  • indices_shape_size - 索引形状的维度大小。

  • indices_shape - 索引的形状数组地址。

  • indices - 索引值地址。

  • core_mask - 核心掩码,指定使用的计算核心。

输出:
  • output - 生成的独热编码结果地址。

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持int8, int16, int32, fp32, fp64, cplx64, cplx128

  • MT7004 支持fp16, fp32, int16, int32, cplx64

  • MT6678 调用时将 axis/depth/support_neg_index/indices_shape_size打包通过 int_param 数组传入

共享存储版本:

void i8_onehot_s(int8_t *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int8_t *output, int core_mask)
void i16_onehot_s(int16_t *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int16_t *output, int core_mask)
void i32_onehot_s(int *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int *output, int core_mask)
void hp_onehot_s(half *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, half *output, int core_mask)
void fp_onehot_s(float *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, float *output, int core_mask)
void dp_onehot_s(double *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, double *output, int core_mask)
void c64_onehot_s(float *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, float *output, int core_mask)
void c128_onehot_s(double *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, double *output, int core_mask)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3
 4int main(int argc, char* argv[]) {
 5    float on_off[2] = {1.0, 0.0};
 6    int axis=1, depth=4, indices_shape_size=2;
 7    int support_neg_index=1;
 8    int int_param[4] = {axis, depth, support_neg_index, indices_shape_size};
 9    int indices_shape[2] = {3, 3};
10    int *indices = (int *)0xA0000000; // indices在DDR空间
11    float *output = (float*)0xB0000000; // indices_shape[0] * depth * indices_shape[1]
12    int core_mask = 0xff;
13    fp_onehot_p(on_off, int_param, indices_shape, indices, output, core_mask);
14    // 7004调用方式:
15    // fp_onehot_p(on_off, axis, depth, support_neg_index, indices_shape_size, indices_shape, indices, output, core_mask);
16    return 0;
17}

私有存储版本:

void i8_onehot_p(int8_t *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int8_t *output)
void i16_onehot_p(int16_t *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int16_t *output)
void i32_onehot_p(int *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, int *output)
void hp_onehot_p(half *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, half *output)
void fp_onehot_p(float *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, float *output)
void dp_onehot_p(double *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, double *output)
void c64_onehot_p(float *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, float *output)
void c128_onehot_p(double *on_off, int axis, int depth, int support_neg_index, int indices_shape_size, int *indices_shape, int *indices, double *output)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3
 4int main(int argc, char* argv[]) {
 5    float on_off[2] = {1.0, 0.0};
 6    int axis=1, depth=4, indices_shape_size=2;
 7    int support_neg_index=1;
 8    int int_param[4] = {axis, depth, support_neg_index, indices_shape_size};
 9    int indices_shape[2] = {3, 3};
10    int *indices = (int *)0x10000000; // indices在L2空间
11    float *output = (float*)0x10001000; // indices_shape[0] * depth * indices_shape[1]
12    fp_onehot_p(on_off, int_param, indices_shape, indices, output);
13    // 7004调用方式:
14    // fp_onehot_p(on_value, off_value, axis, depth, support_neg_index, indices_shape_size, indices_shape, indices, output);
15    return 0;
16}